package com.fingard.dsp.bank.directbank.bochk01;

import com.fingard.dsp.bank.directbank.bankInterface.IBalance;
import com.fingard.dsp.bank.reqrespobj.balance.Req7505HisBalQry;
import com.fingard.dsp.bank.reqrespobj.balance.Req7506BalQry;
import com.fingard.dsp.bank.reqrespobj.balance.RetAts7505HisBalQry;
import com.fingard.dsp.bank.reqrespobj.balance.RetAts7506BalQry;
import com.fingard.dsp.bank.reqrespobj.balance.detail.Req7506Detail;
import com.fingard.dsp.bank.reqrespobj.balance.detail.RetAtsBalDetail;
import com.fingard.text.StringHelper;
import com.fingard.xml.XElement;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

public class BOCHK01Balance extends BOCHK01Base implements IBalance {
    @Override
    public RetAts7506BalQry d_7506_BalQry(Req7506BalQry p_req7506) throws Exception {
        //余额查询返回ATS的对象
        RetAts7506BalQry retAts = new RetAts7506BalQry(p_req7506.reqHeader);
        retAts.writeStart();//写返回ATS的统一头部信息
        try {
            while (p_req7506.readRecord()) {//不支持批量查询，逐个查询
                Req7506Detail tmpReq7506Detail = p_req7506.readedDetail;
                RetAtsBalDetail tmpRetDetail = new RetAtsBalDetail();
                //赋值通用参数
                XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
                BOCHK01Header tmpHeader = new BOCHK01Header();
                tmpHeader.packageId = getQryTransID();
                writeCommonHeader(xmlWriter, tmpHeader);
                XElement tmpXElement = new XElement("AcctBalanceREQ", "");
                tmpXElement.addChild(new XElement("AcctNo", tmpReq7506Detail.actNum));//�~�籼��a
                xmlWriter.writeXElement(tmpXElement);
                xmlWriter.writeEndDocument();

                String[] tmpStrRet = sendToBank(xmlWriter.toXmlString());

                if (tmpStrRet[0].length() == 0) {
                    StringBuilder tmpSbMsg = new StringBuilder();
                    XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
                    if (tmpBkReader != null) {
                        if ("S".equals(tmpBkReader.getFirstText("/BOCHKB2E/Head/TxStatus"))) {
                            String acctCur = convertCurToBank(tmpReq7506Detail.actCur);
                            tmpRetDetail.respCode="0";
                            tmpRetDetail.respInfo="";
                            tmpRetDetail.actNum = tmpBkReader.getFirstText("/BOCHKB2E/Tx/AcctBalanceRLY/AcctNo");
                            if(tmpBkReader.getFirstText("/BOCHKB2E/Tx/AcctBalanceRLY/AcctInfo/SavingAcct")!=null){
                                if (tmpBkReader.read("/BOCHKB2E/Tx/AcctBalanceRLY/AcctInfo/SavingAcct")) {//�π�艨�
                                    if (acctCur.toUpperCase().equals(tmpBkReader.readedXElement.getSingleTextNotNull("Cur"))) {//���
                                        tmpRetDetail.actCur = tmpReq7506Detail.actCur;
                                        tmpRetDetail.currBal = tmpBkReader.readedXElement.getSingleTextNotNull("AcctBalance");//�~�麴N�~
                                        tmpRetDetail.availBal = tmpBkReader.readedXElement.getSingleTextNotNull("AvailBalance");//可用�N�~
                                        tmpRetDetail.frozenBal = tmpBkReader.readedXElement.getSingleTextNotNull("HoldBalance");//�鼋Y�~
                                    } else {
                                        tmpRetDetail.respCode = "-1";
                                        tmpRetDetail.respInfo = "所查账户币种（" + acctCur + "）与查得账户币种（" + tmpBkReader.readedXElement.getSingleTextNotNull("Cur") + "）不相符，请核对";
                                    }
                                }
                            }else if(tmpBkReader.getFirstText("/BOCHKB2E/Tx/AcctBalanceRLY/AcctInfo/CurrentAcct")!=null){
                                if(tmpBkReader.read("/BOCHKB2E/Tx/AcctBalanceRLY/AcctInfo/CurrentAcct")){
                                    if (acctCur.toUpperCase().equals(tmpBkReader.readedXElement.getSingleTextNotNull("Cur"))) {//���
                                        tmpRetDetail.actCur = tmpReq7506Detail.actCur;
                                        tmpRetDetail.currBal = tmpBkReader.readedXElement.getSingleTextNotNull("LedgerBalance");//�~�麴N�~
                                        tmpRetDetail.availBal = tmpBkReader.readedXElement.getSingleTextNotNull("AvailBalance");//可用�N�~
                                        tmpRetDetail.frozenBal = tmpBkReader.readedXElement.getSingleTextNotNull("HoldBalance");//�鼋Y�~
                                    } else {
                                        tmpRetDetail.respCode = "-1";
                                        tmpRetDetail.respInfo = "所查账户币种（" + acctCur + "）与查得账户币种（" + tmpBkReader.readedXElement.getSingleTextNotNull("Cur") + "）不相符，请核对";
                                    }
                                }
                            } else if (tmpBkReader.getFirstText("/BOCHKB2E/Tx/AcctBalanceRLY/AcctInfo/MTCSavingAcct") != null) {//外�R��
                                boolean hasMatchCur = false;
                                String existCur = "";
                                while (tmpBkReader.read("/BOCHKB2E/Tx/AcctBalanceRLY/AcctInfo/MTCSavingAcct/Result/Record")) {
                                    if (acctCur.toUpperCase().equals(tmpBkReader.readedXElement.getSingleTextNotNull("Cur"))) {//���
                                        hasMatchCur = true;
                                        tmpRetDetail.actCur = tmpReq7506Detail.actCur;
                                        tmpRetDetail.currBal = tmpBkReader.readedXElement.getSingleTextNotNull("AcctBalance");//�~�麴N�~
                                        tmpRetDetail.availBal = tmpBkReader.readedXElement.getSingleTextNotNull("AvailBalance");//可用�N�~
                                        tmpRetDetail.frozenBal = tmpBkReader.readedXElement.getSingleTextNotNull("HoldBalance");//�鼋Y�~
                                        break;
                                    } else {
                                        existCur = existCur + tmpBkReader.readedXElement.getSingleTextNotNull("Cur") + ",";
                                    }
                                }
                                if (!hasMatchCur) {
                                    tmpRetDetail.respCode = "-1";
                                    tmpRetDetail.respInfo = "所查账户币种（" + acctCur + "）与查得账户币种（" + StringHelper.trimEnd(existCur, ",") + "）不相符，请核对";
                                }
                            } else {
                                tmpRetDetail.respCode = "-1";
                                tmpRetDetail.respInfo = "银行响应数据非法!";
                            }
                        } else {
                            tmpRetDetail.respCode = "-1";
                            tmpRetDetail.respInfo = "错误代码：" + tmpBkReader.getFirstText("/BOCHKB2E/Head/ErrorCode") + "，错误信息：" + tmpBkReader.getFirstText("/BOCHKB2E/Head/ErrorDesc");
                        }
                    } else {
                        tmpRetDetail.respCode = "-1";
                        tmpRetDetail.respInfo = tmpSbMsg.toString();
                    }
                } else {
                    tmpRetDetail.respCode = "-1";
                    tmpRetDetail.respInfo = tmpStrRet[1];
                }
                retAts.writeRetDetail(tmpRetDetail, p_req7506.isBatch);
            }
        } catch (Exception e) {
            retAts.writeRespCode("-1");
            retAts.writeRespInfo(e.getMessage());
            e.printStackTrace();
        }
        retAts.writeEnd();
        return retAts;
    }

    @Override
    public RetAts7505HisBalQry d_7505_BalQry(Req7505HisBalQry p_req7505) throws Exception {
        return null;
    }
}
